
Partial Class Modules_Questionnaires_Controls_ansDate
    Inherits System.Web.UI.UserControl

    Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC


    Private _config, _answerType, _questionId, _sortId As String
    Private _dateType As String

    Public Property Answer() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Answer
        Get
            If _dateType = "0" Then
                Return IIf(calFrom.SelectedDate.Year = 1, String.Empty, calFrom.SelectedDate)
            Else
                Return IIf(calFrom.SelectedDate.Year = 1, String.Empty, calFrom.SelectedDate) & "|" & _
                       IIf(calTo.SelectedDate.Year = 1, String.Empty, calTo.SelectedDate)
            End If
        End Get
        Set(ByVal value As String)
            If String.IsNullOrEmpty(value) Then
                Exit Property
            End If
            If _dateType = "0" Then
                calFrom.SelectedDate = value
            Else
                Dim val As String() = value.Split("|")
                calFrom.SelectedDate = val(0)
                calTo.SelectedDate = val(1)
            End If
        End Set
    End Property

    Public Property Config() As String Implements DotNetPM.Business.Questionnaire.Taker.IAnswerUC.Config
        Get
            Return _config
        End Get
        Set(ByVal value As String)
            If Not String.IsNullOrEmpty(value) Then
                _config = value
            End If
        End Set
    End Property

    Private Sub LoadAnswerControl()
        Dim val As String() = Split(Me.Config, "||")
        _sortId = val(0)
        _questionId = val(1)
        _answerType = val(2)
        Dim _isRequired As String = val(3)
        _dateType = val(4)
        Dim _dateMin As String = val(5)
        Dim _dateMax As String = val(6)

        If _isRequired = "0" Then
            Dim reqFrom As New RequiredFieldValidator
            With reqFrom
                .ID = "reqFrom"
                .ControlToValidate = "calFrom"
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "The answer of Date type is required!"
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : The answer of type Date is required."
            End With
            phFrom.Controls.Add(reqFrom)
        End If
        If Not String.IsNullOrEmpty(_dateMin) AndAlso Not String.IsNullOrEmpty(_dateMax) Then
            Dim rangeFrom As New RangeValidator
            With rangeFrom
                .ID = "rangeFrom"
                .Type = ValidationDataType.Date
                .MinimumValue = CDate(_dateMin)
                .MaximumValue = CDate(_dateMax)
                .ControlToValidate = "calFrom"
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid date value! The valid value is between " & _dateMin & " and " & _dateMax & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the valid value is between " & _dateMin & " to " & _dateMax & "."
            End With
            phFrom.Controls.Add(rangeFrom)
        ElseIf Not String.IsNullOrEmpty(_dateMin) Then
            Dim compFrom As New CompareValidator
            With compFrom
                .ID = "compFrom"
                .ControlToValidate = "calFrom"
                .Operator = ValidationCompareOperator.GreaterThanEqual
                .Type = ValidationDataType.Date
                .ValueToCompare = CDate(_dateMin)
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid date value! The minimum value is " & _dateMin & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the minimum value is " & _dateMin & "."
            End With
            phFrom.Controls.Add(compFrom)
        ElseIf Not String.IsNullOrEmpty(_dateMax) Then
            Dim compFrom As New CompareValidator
            With compFrom
                .ID = "compFrom"
                .ControlToValidate = "calFrom"
                .Operator = ValidationCompareOperator.LessThanEqual
                .Type = ValidationDataType.Date
                .ValueToCompare = CDate(_dateMax)
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid date value! The maximum value is " & _dateMax & "."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the maximum value is " & _dateMax & "."
            End With
            phFrom.Controls.Add(compFrom)
        End If

        If _dateType = "0" Then
            tdFrom.InnerHtml = "Date :"
            lblTo.Visible = False
            calTo.Visible = False
            phTo.Visible = False
        Else
            tdFrom.InnerHtml = "From :"
            If _isRequired = "0" Then
                Dim reqTo As New RequiredFieldValidator
                With reqTo
                    .ID = "reqTo"
                    .ControlToValidate = "calTo"
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "*"
                    .ErrorMessage = "The answer of Date type is required."
                    '.ErrorMessage = "Question# <b>" & _questionId & "</b> : The answer of type Date is required."
                End With
                phTo.Controls.Add(reqTo)
            End If
            Dim compVal As New CompareValidator
            With compVal
                .ID = "compTo"
                .ControlToValidate = "calTo"
                .ControlToCompare = "calFrom"
                .Operator = ValidationCompareOperator.GreaterThanEqual
                .Type = ValidationDataType.Date
                .Display = ValidatorDisplay.Dynamic
                .Text = "*"
                .ErrorMessage = "Invalid date value! The Maximum value cannot be less than the Minimum value."
                '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the Maximum value cannot be less than the Minimum value."
                '.Text = "*"
            End With
            phTo.Controls.Add(compVal)
            If Not String.IsNullOrEmpty(_dateMin) AndAlso Not String.IsNullOrEmpty(_dateMax) Then
                Dim rangeTo As New RangeValidator
                With rangeTo
                    .ID = "rangeTo"
                    .Type = ValidationDataType.Date
                    .MinimumValue = CDate(_dateMin)
                    .MaximumValue = CDate(_dateMax)
                    .ControlToValidate = "calTo"
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "*"
                    .ErrorMessage = "Invalid date value! The valid value is between " & _dateMin & " to " & _dateMax & "."
                    '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the valid value is between " & _dateMin & " to " & _dateMax & "."
                End With
                phTo.Controls.Add(rangeTo)
            ElseIf Not String.IsNullOrEmpty(_dateMin) Then
                Dim compTo As New CompareValidator
                With compTo
                    .ID = "compTo"
                    .ControlToValidate = "calTo"
                    .Operator = ValidationCompareOperator.GreaterThanEqual
                    .Type = ValidationDataType.Date
                    .ValueToCompare = CDbl(_dateMin)
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "*"
                    .ErrorMessage = "Invalid date value! The minimum value is " & _dateMin & "."
                    '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the minimum value is " & _dateMin & "."
                End With
                phTo.Controls.Add(compTo)
            ElseIf Not String.IsNullOrEmpty(_dateMax) Then
                Dim compTo As New CompareValidator
                With compTo
                    .ID = "compTo"
                    .ControlToValidate = "calTo"
                    .Operator = ValidationCompareOperator.LessThanEqual
                    .Type = ValidationDataType.Date
                    .ValueToCompare = CDbl(_dateMax)
                    .Display = ValidatorDisplay.Dynamic
                    .Text = "*"
                    .ErrorMessage = "Invalid date value. The maximum value is " & _dateMax & "."
                    '.ErrorMessage = "Question# <b>" & _questionId & "</b> : Invalid Date Value, the maximum value is " & _dateMax & "."
                End With
                phTo.Controls.Add(compTo)
            End If
        End If
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadAnswerControl()
    End Sub

    Protected Sub Page_PreRender(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreRender
        'LoadAnswerControl
    End Sub

End Class
